<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card class="box-card">
      <!-- 搜索与添加区域 -->
      <el-row :gutter="30">
        <el-col :span="8">
          <el-input placeholder="请输入内容"
                    v-model="queryInfo.query"
                    clearable
                    @clear="getGoodsList">
            <el-button slot="append"
                       icon="el-icon-search"
                       @click="getGoodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary"
                     @click="goAddPage">添加商品</el-button>
        </el-col>
      </el-row>

      <el-table :data="goodsList"
                border
                stripe>
        <el-table-column type="index"
                         label="#"></el-table-column>
        <el-table-column label="商品名称"
                         prop="goods_name"></el-table-column>
        <el-table-column label="商品价格(元)"
                         prop="goods_price"
                         width="120px"></el-table-column>
        <el-table-column label="商品重量"
                         prop="goods_weight"
                         width="80px"></el-table-column>
        <el-table-column label="创建时间"
                         width="200px">
          <template v-slot="scope">
            {{scope.row.add_time | dateFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作"
                         width="125px">
          <template v-slot="scope">
            <el-button type="primary"
                       icon="el-icon-edit"
                       size="mini"
                       @click="showEditDialog(scope.row.goods_id)"></el-button>
            <el-button type="danger"
                       icon="el-icon-delete"
                       size="mini"
                       @click="removeById(scope.row.goods_id)"></el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination @size-change="handleSizeChange"
                     @current-change="handleCurrentChange"
                     :current-page="queryInfo.pagenum"
                     :page-sizes="[3, 5, 10]"
                     :page-size="queryInfo.pagesize"
                     layout="total, sizes, prev, pager, next, jumper"
                     :total="total">
      </el-pagination>
    </el-card>

    <!-- 编辑对话框 -->
    <el-dialog title="编辑商品信息"
               :visible.sync="editDialogVisible"
               width="50%">
      <el-form :model="editForm"
               :rules="formRules"
               ref="editFormRef"
               label-width="80px">
        <el-form-item label="商品名称"
                      prop="goods_name">
          <el-input v-model="editForm.goods_name"></el-input>
        </el-form-item>
        <el-form-item label="商品价格"
                      prop="goods_price">
          <el-input v-model="editForm.goods_price"></el-input>
        </el-form-item>
        <el-form-item label="商品重量"
                      prop="goods_weight">
          <el-input v-model="editForm.goods_weight"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer"
            class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="editGoods">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { goodsList, deleteGoods, queryGoods, editGoodsSubmit } from '../../network/goodsList';
export default {
  name: 'GoodsList',
  data () {
    return {
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      goodsList: [],
      total: 0,
      editDialogVisible: false,
      //查询到的商品信息
      editForm: {},
      formRules: {
        goods_name: { required: true, message: '请输入商品名称', trigger: 'blur' },
        goods_price: { required: true, message: '商品价格必须大于0', trigger: 'blur' },
        goods_weight: { required: true, message: '商品重量必须大于0', trigger: 'blur' },
      }
    }
  },
  created () {
    this.getGoodsList();
  },
  methods: {
    async getGoodsList () {
      const res = await goodsList(this.queryInfo);
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品列表失败');
      }
      this.goodsList = res.data.goods;
      this.total = res.data.total;
    },

    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize;
      this.getGoodsList();
    },

    handleCurrentChange (newPage) {
      this.queryInfo.pagenum = newPage;
      this.getGoodsList();
    },

    async removeById (id) {
      const confirmResult = await this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err);
      if (confirmResult !== 'confirm') {
        return this.$message.info('已经取消了删除');
      }
      const res = await deleteGoods(id);
      if (res.meta.status !== 200) {
        return this.$message.error('删除失败');
      }
      this.$message.success('删除成功');
      this.getGoodsList();
    },

    goAddPage () {
      this.$router.push('/goods/add')
    },

    async showEditDialog (goods_id) {
      const res = await queryGoods(goods_id);
      this.editForm = res.data;
      this.editDialogVisible = true;
    },

    editGoods () {
      this.$refs.editFormRef.validate(async valid => {
        if (!valid) return;
        const res = await editGoodsSubmit(this.editForm);
        if (res.meta.status !== 200) {
          return this.$message.error('商品更新失败');
        }
        this.editDialogVisible = false;
        this.getGoodsList();
        this.$message.success('更新商品成功');
      })
    }
  },
}
</script>

<style lang="less" scoped>
.el-table {
  margin: 20px 0;
}
</style>